package com.sczmgk.hotline_pzh;

import android.app.ActivityManager;
import android.app.Application;
import android.content.pm.PackageManager;
import android.util.Log;

import com.android.bitmapfun.util.MyImageFetcher;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.lidroid.xutils.BitmapUtils;
import com.linkrenhe.frame.http.HttpRequest;
import com.linkrenhe.frame.http.TokenCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.hxim.HxImUtils;
import com.sczmgk.hotline_pzh.location.LocationService;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.FileUtil;
import com.sczmgk.hotline_pzh.utils.MyLog;
import com.sczmgk.hotline_pzh.utils.PictureUtil;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class MyApp extends Application {
	//TODO
	public static LocationService locationService;
	/**
	 * 语音听写对象
	 */
//	public static SpeechRecognizer mIat;
	
	public static HxImUtils hxImUtils;
	/**
	 * 登录的用户名
	 */
	public static String loginUserName;
	/**
	 * 登录的用户ID
	 */
	public static int loginUserUid;

	public static int appointmentcoord;
	public static boolean isZhang = true;
	public static String sessionKey;
	public static String randomCode;
	public static int type;
	public static String token;
	public static String imei ;
	public static String imsi = "0000";
//	public static String hotLine = "96166";
	private static String TAG = MyApp.class.getSimpleName();

	public static BitmapUtils bitmapUtils ;

	@Override
	public void onCreate() {
		super.onCreate();

		bitmapUtils = new BitmapUtils(getApplicationContext()) ;
//		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_center);
		bitmapUtils.configDefaultBitmapMaxSize(200,200);

		//社会化分享
		PlatformConfig.setWeixin("wxc3d811637d27f949","646321950173005a523ecf9a74116eee");
		PlatformConfig.setQQZone("1105799497","NsPsuMNxS6M9P7QD");
//		PlatformConfig.setSinaWeibo("2823425374","d7aad2871f33894b7023eb77e7a804af");
		UMShareAPI.get(this);
//将自定义的异常设置给主线程


		MyCrashHandler crashHandler = MyCrashHandler.getInstance();
		crashHandler.setContext(getApplicationContext());
		Thread.currentThread().setUncaughtExceptionHandler(crashHandler);

	////////////////////////////////////////////////////////////////////////////
		initUserInfo();
		imei= CommonUtils.getPhoneImei(getApplicationContext());
//		dm = new DBManager(getApplicationContext());
//		serviceAreaCode = CacheUtils.getStringData(this, Constants.AREA_CODE_KEY, "0104");
		//初始化文件存储路径
		FileUtil.initAppStoragePath(this);
		//初始化图片尺寸
		PictureUtil.initImageMeasureSize(this);
		initImageFeather();
		initIM();
		hxImUtils = new HxImUtils(getApplicationContext());
//		MscUtils.getInstance().initMsc(getApplicationContext());

//		hxImUtils.initIm();
		/***
		 * 初始化定位sdk，建议在Application中创建
		 */
		locationService = new LocationService(getApplicationContext());
//		SDKInitializer.initialize(getApplicationContext());
	}

	/**
	 * 初始化图片获取器
	 */
	private void initImageFeather() {
	MyImageFetcher.init(getApplicationContext(), new TokenCallback() {
			
			@Override
			public HashMap<String, String> getToken(HttpRequest httpRequest) {
				return null;
			}
			
			@Override
			public String getDownloadUrl(String fileName, String styleName) {
				return null;
			}
		});
	}

	/**
	 * 初始化环信
	 */
	private void initIM() {
		int pid = android.os.Process.myPid();
		String processAppName = getAppName(pid);
		// 如果app启用了远程的service，此application:onCreate会被调用2次
		// 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
		// 默认的app会在以包名为默认的process name下运行，如果查到的process name不是app的process name就立即返回

		if (processAppName == null ||!processAppName.equalsIgnoreCase("com.sczmgk.hotline_pzh")) {
		    Log.e(TAG, "enter the service process!");
		    //"com.easemob.chatuidemo"为demo的包名，换到自己项目中要改成自己包名
		    
		    // 则此application::onCreate 是被service 调用的，直接返回
		    return;
		}
		//环信初始化
		EMOptions options = new EMOptions();
		// 默认添加好友时，是不需要验证的，改成需要验证
		options.setAcceptInvitationAlways(false);

		//初始化
		EMClient.getInstance().init(getApplicationContext(), options);
		//在做打包混淆时，关闭debug模式，避免消耗不必要的资源
		EMClient.getInstance().setDebugMode(false);
	}

	/**
	 * 取得app名
	 * @param pID
	 * @return
	 */
	private String getAppName(int pID) {
		String processName = null;
		ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		PackageManager pm = this.getPackageManager();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
			try {
				if (info.pid == pID) {
					CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
					// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
					// info.processName +"  Label: "+c.toString());
					// processName = c.toString();
					processName = info.processName;
					return processName;
				}
			} catch (Exception e) {
				// Log.d("Process", "Error>> :"+ e.toString());
			}
		}
		return processName;
	}
	/**
	 * 如果是已登录，则取出UID
	 */
	private void initUserInfo() {
		MyLog.d(TAG,"取出登录状态："+CacheUtils.getBooleanData(getApplicationContext(), Constants.IS_LOGIN_KEY, false));
		if(CacheUtils.getBooleanData(getApplicationContext(), Constants.IS_LOGIN_KEY, false)){


			loginUserUid = CacheUtils.getIntData(getApplicationContext(), Constants.LOGIN_UID, 0);
			if(loginUserUid==0 ){
				CacheUtils.cacheBooleanData(getApplicationContext(), Constants.IS_LOGIN_KEY, false);
			}
			MyLog.e(TAG,"不需要登录，调用单点方法----------");
		}
		
	}
}
